package com.think.teaching.controller;


import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.PaperQuestion;
import com.think.teaching.model.PaperZone;
import com.think.teaching.model.Question;
import com.think.teaching.service.impl.PaperQuestionServiceImpl;
import com.think.teaching.service.impl.PaperZoneServiceImpl;
import com.think.teaching.service.impl.QuestionServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 试卷和试题关联 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "试卷试题管理接口")
public class PaperQuestionController {
	
	private final PaperQuestionServiceImpl paperQuestionService;
	private final QuestionServiceImpl questionService;
	private final PaperZoneServiceImpl paperZone;

	public PaperQuestionController(PaperQuestionServiceImpl paperQuestionService,
								   QuestionServiceImpl questionService,
								   PaperZoneServiceImpl paperZone) {
		this.paperQuestionService = paperQuestionService;
		this.questionService=questionService;
		this.paperZone=paperZone;
	}

	@ApiOperation("添加新的试卷试题")
	@PostMapping("admin/paperQuestion")
	public Result AddNewPaperQuestion(@RequestBody()PaperQuestion data) {
			Result result = data.CheckObjectComplete();
			if (result.getCode() == ResultCode.SUCCESS.code()) {
				Question question=questionService.getById(data.getQuestionId());
				if(question==null)
					return Result.failure(ResultCode.QUESTION_NOT_EXIST);
				PaperZone zone = paperZone.getById(data.getZoneId());
				if (zone == null) {
					return Result.failure(ResultCode.PAPER_ZONE_NOT_EXIST);
				}
				if (!zone.getType().equals(question.getType()))
					return Result.failure(ResultCode.QUESTION_TYPE_NOT_MATCH);
				if(paperQuestionService.CheckExisted(data.getQuestionId(),
						data.getZoneId()))
					return Result.failure(ResultCode.PAPER_QUESTION_ALREADY_EXIST);
				PaperQuestion paperQuestion = paperQuestionService.AddNewPaperQuestion(data);
				if (paperQuestion != null) {
					return Result.success(paperQuestion);
				} else {
					return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
				}
			} else
				return result;
	}

	
	@ApiOperation("更新试卷试题")
	@RequestMapping(value = "admin/paperQuestion",method = RequestMethod.PUT)
	public Result UpdatePaperQuestion(@RequestBody()PaperQuestion data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			PaperQuestion paperQuestion = paperQuestionService.UpdatePaperQuestion(data);
			if (paperQuestion != null) {
				return Result.success(paperQuestion);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("删除试卷试题")
	@DeleteMapping("admin/paperQuestion/{id}")
	public Result DeletePaperQuestion(@PathVariable("id")Integer id) {
			if (paperQuestionService.DeleteObjectById(id)) {
				return Result.success();
			}else
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	
	@ApiOperation("管理员获取试卷试题详情")
	@RequestMapping(value = "admin/paperQuestion/{id}",method = RequestMethod.GET)
	public Result GetPaperQuestion(@PathVariable("id")Integer id) {
		PaperQuestion paperQuestion = paperQuestionService.getById(id);
		if (paperQuestion == null) {
			return Result.failure(ResultCode.PAPER_QUESTION_NOT_EXIST);
		} else {

			return Result.success(paperQuestion);
		}
	}

	@ApiOperation("根据分区id获取试卷试题列表")
	@GetMapping( "admin/paperQuestion/zone/{id}")
	public Result GetPaperQuestionByZone(@PathVariable("id")Integer id) {
		return Result.success(paperQuestionService.GetPaperQuestionByZone(id));
	}
	
	@ApiOperation("管理员修改试卷试题状态")
	@RequestMapping(value = "admin/paperQuestion/{id}/{state}",method = RequestMethod.PUT)
	public Result UpdatePaperQuestionState(@PathVariable("id")Integer id,@PathVariable("state")Integer state) {
		PaperQuestion paperQuestion= paperQuestionService.getById(id);
		if (paperQuestion == null) {
			return Result.failure(ResultCode.PAPER_QUESTION_NOT_EXIST);
		} else {
			try {
				paperQuestion = paperQuestionService.SetPaperQuestionState(id, state);
				return Result.success(paperQuestion);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
}

